package com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyAdapter.SignIntegralActivityAdapter;
import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.util.view.HollowPieChart;
import com.diudiutechnology.www.yadiudiu.util.view.PieDataEntity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity.LoginActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.SignIntegralModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/8/30.
 * 授信积分
 */

public class CreditScoreActivity extends Activity implements View.OnClickListener ,Callback.CommonCallback<String>{
    @Bind(R.id.retur)
    LinearLayout retur;

    @Bind(R.id.listview)
    ListView listview;
    List<SignIntegralModel> signIntegralModels;
    SignIntegralActivityAdapter adapter;

    LinearLayout earn_points_layout;//赚积分

    TextView integral;//总积分
    TextView consumption;//平台消费积分
    TextView check;//每日消费积分
    TextView total;//总计

    private HollowPieChart chart;//圈
    Context context;
    private MyProgressDialog mDialog;
    final List<PieDataEntity> dataEntities = new ArrayList<>();
    String eachcheckin ="0";
    String getpoints="0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_score);
        ButterKnife.bind(this);
        context=this;
        initView();
    }

    private void initView() {
        retur.setOnClickListener(this);
        View  vHead= View.inflate(context, R.layout.credit_score_listviewaddheaderview, null);

        earn_points_layout= (LinearLayout) vHead.findViewById(R.id.earn_points_layout);//赚积分
        integral= (TextView) vHead.findViewById(R.id.integral);//总积分
        consumption= (TextView) vHead.findViewById(R.id.consumption);//平台消费积分
        check= (TextView) vHead.findViewById(R.id.check);//每日消费积分
        total= (TextView) vHead.findViewById(R.id.total);//总计
        chart= (HollowPieChart) vHead.findViewById(R.id.chart);//圈
        earn_points_layout.setOnClickListener(this);
        listview.addHeaderView(vHead);
        adapter = new SignIntegralActivityAdapter(context);
        listview.setAdapter(adapter);
        initData();

    }
    private void initData() {
        PieDataEntity enti = new PieDataEntity("0",360,0xff448ACA);
        dataEntities.add(enti);
        chart.setDataList(dataEntities);
    }

    @Override
    protected void onResume() {
        super.onResume();
        processcreditScore();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.retur:
                finish();
                break;
            case R.id.earn_points_layout:
                Intent intent = new Intent(context, EarnPointsActivity.class);
                intent.putExtra("eachcheckin",eachcheckin);
                intent.putExtra("getpoints",getpoints);
                startActivity(intent);
                break;



        }
    }
    //用户授信积分详情页面
    private void processcreditScore() {
        if (Sundry.isOpenNetwork(context)) {
            mDialog = new MyProgressDialog(context);

            HttpClient.creditScore(this);
        } else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }

    @Override
    public void onSuccess(String result) {
        if (mDialog != null) {
            mDialog.dismiss();
        }
        System.out.println("stringmd5result" + result);
        List<RegistrationModel> getnote = JsonTools.getunderlying(result);
        if (getnote != null || getnote.size() > 0) {
            String status = getnote.get(0).getStatus();
            String getMsg = getnote.get(0).getMsg();
            if (status.equals("200")) {

                String creditscore = getnote.get(0).getCreditscore();
                String consumerpoints = getnote.get(0).getConsumerpoints();
                String signintegral = getnote.get(0).getSignintegral();
                 eachcheckin = getnote.get(0).getEachcheckin();
                 getpoints = getnote.get(0).getGetpoints();
                String content = getnote.get(0).getContent();
                integral.setText(creditscore);
                consumption.setText(consumerpoints);
                check.setText(signintegral);

                int consum=0;
                if (!Sundry.getStinges(consumerpoints)){
                    consum=Integer.valueOf(consumerpoints);
                }
                int sign=0;
                if (!Sundry.getStinges(signintegral)){
                    sign=Integer.valueOf(signintegral);
                }
                String allsum=(consum+sign)+"";
                total.setText(allsum);
                PieDataEntity entity = new PieDataEntity(allsum,consum,0xffF35944);
                dataEntities.add(entity);
                PieDataEntity enti = new PieDataEntity(allsum,sign,0xff448ACA);
                dataEntities.add(enti);
                chart.setDataList(dataEntities);
                if (!TextUtils.isEmpty(content)){
                List<SignIntegralModel> signIntegralModels = JsonTools.getcreditScore(content);
                if (signIntegralModels != null || signIntegralModels.size() > 0) {
                    for (int i=0;i<signIntegralModels.size();i++){
                        adapter.addItem(signIntegralModels.get(i),1);
                    }
                }
              }else {
                    ToastUtils.show(context, getMsg);
                }
            } else if (status.equals("105")) {
                Intent intent = new Intent(context, LoginActivity.class);
                startActivity(intent);
                ToastUtils.show(context, getMsg);
            } else {
                ToastUtils.show(context, getMsg);
            }
        }
    }

    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + ex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onCancelled(CancelledException cex) {
        if (mDialog != null) {
            mDialog.dismiss();
        }

        System.out.println("stringmd5result" + cex.getMessage().toString());
        ToastUtils.Toastshow(context, getResources().getString(R.string.server));
    }

    @Override
    public void onFinished() {

    }
}

